package top.mccsdl.Acbot.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import top.mccsdl.Acbot.service.TaskToken;

@Component
public class BotConfig {

    private static final int REFRESH_BUFFER_SECONDS = 300;
    public static String APPID;

    public static String TOKEN;

    public static String APP_SECRET;

    public static String ACCESS_TOKEN;
    public static Integer EXPIRES_IN;

    public static long TOKEN_FETCH_TIME;
    @Autowired
    private TaskToken taskToken;

    public String getAccessToken() {
        if (isTokenExpired()) {
            taskToken.fetchAndScheduleRefresh();
        }
        return ACCESS_TOKEN;
    }

    private boolean isTokenExpired() {
        long currentTime = System.currentTimeMillis() / 1000;
        long elapsedTime = currentTime - TOKEN_FETCH_TIME;
        return EXPIRES_IN == null || EXPIRES_IN == 0 || elapsedTime >= EXPIRES_IN;
    }

    @Value("${bot.appid}")
    public void setAPPID(String APPID) {
        BotConfig.APPID = APPID;
    }

    @Value("${bot.token}")
    public void setTOKEN(String TOKEN) {
        BotConfig.TOKEN = TOKEN;
    }

    @Value("${bot.secret}")
    public void setAppSecret(String APP_SECRET) {
        BotConfig.APP_SECRET = APP_SECRET;
    }
//提交审核的时候记得切换成正式地址 否则审核不通过
    // "api.sgroup.qq.com" "sandbox.api.sgroup.qq.com" 沙盒测试
//    public static final String DOMAIN = "https://sandbox.api.sgroup.qq.com";
    public static final String API_URL = "https://bots.qq.com/app/getAppAccessToken";
    public static final String MCCSDL = "https://xxxxxxxxx:8080"; //当前服务API地址 生成图片啥的使用
//正式
    public static final String DOMAIN = "https://api.sgroup.qq.com";

    // /v2/groups/{group_openid}/messages
    public static final String POST_GROUP_MESSAGE = "/v2/groups/%s/messages";

    // /channels/{channel_id}/messages
    public static final String POST_CHANNEL_MESSAGE = "/channels/%s/messages";

    // /v2/users/{openid}/messages
    public static final String POST_PRIVATE_MESSAGE = "/v2/users/%s/messages";

    /**
     * 发送文件 富媒体消息
     * <p>
     * file_type	int	是	媒体类型：1 图片，2 视频，3 语音，4 文件（暂不开放） 资源格式要求
     * 图片：png/jpg，视频：mp4，语音：silk url	string	是	需要发送媒体资源的url srv_send_msg	bool	是
     * 设置 true 会直接发送消息到目标端，且会占用主动消息频次 file_data	否	【暂未支持】
     */
    // /v2/users/{openid}/files 发送私聊文件
    public static final String POST_PRIVATE_FILE = "/v2/users/%s/files";

    // /v2/groups/{group_openid}/files 发送群聊文件
    public static final String POST_GROUP_FILE = "/v2/groups/%s/files";
    //

    //频道
    // /dms/{guild_id}/messages 频道私信
    public static final String POST_CHANNELS_PRI_MSG = "/dms/%s/messages";
    // /channels/{channel_id}/messages 频道消息(文字子频道)
    public static final String POST_CHANNELS_PUB_MSG = "/channels/%s/messages";
    /**
     * 频道表情表态 PUT
     * /channels/{channel_id}/messages/{message_id}/reactions/{type}/{id}
     */
    public static final String PUT_CHANNELS_MSG_EMOTE = "/channels/%s/messages/%s/reactions/%s/%s";
}
